package samples;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import agent.AgentBoard;
import agent.AgentHand;
import agent.DominoAgent;
import core.basic.Movement;
import core.basic.Piece;

public class HumanAgent implements DominoAgent {

	@Override
	public Movement getMovement(AgentHand yourHand, AgentBoard currentBoard) {
		Movement res = new Movement();
		System.out.println(">>> It's your turn!!!\n"
				+ "* Table is like this:\n" + currentBoard.toString() + "\n"
				+ "* And this is your current hand:\n" + yourHand.toString()
				+ "\n");

		System.out
				.println("* Please select one of the following choices for your next moviment: "
						+ "   1 - to choose a Piece.\n"
						+ "   2 - to pass.\n\n"
						+ "* Please type the number of your choise: ");

		String choice = "";
		try {
			BufferedReader in = new BufferedReader(new InputStreamReader(
					System.in));

			int tries = 5;

			while (choice != "1" && choice != "2" && tries >= 0) {
				if (tries == 0) {
					choice = "2";
					tries--;
					break;
				}
				tries--;

				choice = in.readLine();
				choice = choice.trim();

				if (choice != "1" && choice != "2") {
					System.out.println("* You've to type '1' or '2'!! (You've "
							+ tries + " more attempts).\n");
					System.out
							.println("* Please select one of the following choices for your next moviment: "
									+ "   1 - to choose a Piece.\n"
									+ "   2 - to pass.\n\n"
									+ "* Please type the number of your choise: ");
				}
			}
			System.out.println(choice);
		} catch (IOException e) {
			System.out.println(e.toString());
		}

		if (choice == "1") {
			System.out
					.println("* Choose one of the pieces available by typing one of the piece's number, from "
							+ Integer.valueOf(0)
							+ " to "
							+ Integer.valueOf(yourHand.size() - 1)
							+ " above:\n");
			for (int i = 0; i < yourHand.size(); i++) {// mostra as pedras
				// disponiveis com o seu
				// indice
				System.out.println("Piece number " + Integer.valueOf(i) + ": "
						+ yourHand.getPiece(i).toString());
			}
			try {
				BufferedReader in = new BufferedReader(new InputStreamReader(
						System.in));
				int tries = 5;
				choice = "-1";

				while (Integer.getInteger(choice) < 0
						|| Integer.getInteger(choice) > yourHand.size() - 1
						&& tries >= 0) {
					if (tries == 0) {// verifica se ainda ha tentativa
						// disponivel, caso n, ele
						// obrigatoriamente irah passar
						res.pass = true;
						tries--;
						break;
					}
					tries--;

					choice = in.readLine();
					choice = choice.trim();

					if (Integer.getInteger(choice) < 0
							|| Integer.getInteger(choice) > yourHand.size() - 1) {// se
						// a
						// escolha
						// n
						// estiver
						// dentro
						// do
						// range
						// de
						// pedras...
						System.out.println("* You've to type a number from "
								+ Integer.valueOf(0) + " to "
								+ Integer.valueOf(yourHand.size() - 1)
								+ " (You've " + tries + " more attempts).\n");

						System.out
								.println("* Choose one of the pieces available by typing one of the piece's number, from "
										+ Integer.valueOf(0)
										+ " to "
										+ Integer.valueOf(yourHand.size() - 1)
										+ " above:\n");
						for (int i = 0; i < yourHand.size(); i++) {// mostra as
							// pedras
							// disponiveis com o seu
							// indice
							System.out.println("Piece number "
									+ Integer.valueOf(i) + ": "
									+ yourHand.getPiece(i).toString());
						}
					} else {
						try {
							// BufferedReader in = new BufferedReader(new
							// InputStreamReader(System.in));
							choice = "not choosen yet";

							while (choice != "l" && choice != "r") {
								System.out
										.println("* What's the side of the table that you want to play? (Type l for left / Type r for right)");
								choice = in.readLine();
								choice = choice.trim();
								if (choice != "l" && choice != "r") {
									System.out
											.println("* You must trype l or r\n");
								} else {
									res.pass = false;
									res.p = yourHand.getPiece(Integer
											.getInteger(choice));
									if (choice == "l") {
										res.side = true;
									} else if (choice == "r") {
										res.side = false;
									}
								}
							}

						} catch (Exception e) {
							System.out.println(e.toString());
						}
					}
				}

			} catch (Exception e) {
				System.out.println(e.toString());
			}

		} else if (choice == "2") {//se entrar aqui eh pq o usuario ou escolheu passar, ou entao n acertou nas 5 tentativas oq queria fazer!!!
			res.pass = true;
		} else
			System.out.println("Something's wrong... check the code!!!");

		return res;
	}

	@Override
	public void receiveInfo(Movement m, DominoAgent playerAgent) {
	}

	@Override
	public Movement wrongMovement(Movement wrongMovement) {
		wrongMovement.side = false;
		return wrongMovement;
	}

}